let FORWARD = 1;
let BACKWARD = 2;
let RIGHT = 3;
let LEFT = 4;
let STOP = 0;
let WAITING = 5;

let but_forward = document.getElementById('but0')
let but_backward = document.getElementById('but1')
let but_right = document.getElementById('but2')
let but_left = document.getElementById('but3')
let p = document.getElementById('p')

let RUN = "http://192.168.5.1/run"
let GET_DOG_STATE = "http://192.168.5.1/getDogState"

let i = 0;
let run_code = 0;
let run_code_tmp = run_code;

//let isServoRuning=false;



function sendRunCode() {

    let sendRequest = new XMLHttpRequest();
    sendRequest.open("GET", RUN + '?run_code=' + run_code, true);
    sendRequest.send(null);


    sendRequest.onreadystatechange = function () {
        if (sendRequest.readyState === 4) {
            if (sendRequest.status === 200) {

            } else {
                console.error('Request failed with status:', sendRequest.status);

            }
        }
    };

    run_code %= 10;
    run_code_tmp = run_code;
}
function runIntervalNTimes(time, n, callback) {
    let count = 0;
    const intervalId = setInterval(() => {
        callback();
        count++;
        if (count === n) {
            clearInterval(intervalId);
        }
    }, time); // 这里设置的是每 1000 毫秒（1 秒）执行一次回调函数，可以根据实际需求调整
}


function handleMouseDown(code) {
    return function () {
        run_code = code;
        p.innerHTML = "touchstart" + run_code;
        sendRunCode();
    };
}

function handleMouseUp() {
    return function () {
        run_code = STOP;
        p.innerHTML = "touchend" + run_code;
        sendRunCode();
    };
}



//适配于手机
but_forward.addEventListener("touchstart", handleMouseDown(FORWARD));
but_forward.addEventListener("touchend", handleMouseUp());

but_backward.addEventListener("touchstart", handleMouseDown(BACKWARD));
but_backward.addEventListener("touchend", handleMouseUp());

but_right.addEventListener("touchstart", handleMouseDown(RIGHT));
but_right.addEventListener("touchend", handleMouseUp());

but_left.addEventListener("touchstart", handleMouseDown(LEFT));
but_left.addEventListener("touchend", handleMouseUp());

// //适配于电脑
but_forward.addEventListener("mousedown", handleMouseDown(FORWARD));
but_forward.addEventListener("mouseup", handleMouseUp());

but_backward.addEventListener("mousedown", handleMouseDown(BACKWARD));
but_backward.addEventListener("mouseup", handleMouseUp());

but_right.addEventListener("mousedown", handleMouseDown(RIGHT));
but_right.addEventListener("mouseup", handleMouseUp());

but_left.addEventListener("mousedown", handleMouseDown(LEFT));
but_left.addEventListener("mouseup", handleMouseUp());



document.addEventListener("DOMContentLoaded", () => {
    // location.assign('./test.html');

});
//100ms 就请求一次看看是不是充电中
setInterval(() => {
    let sendRequest = new XMLHttpRequest();
    sendRequest.open("GET", GET_DOG_STATE, true);
    sendRequest.send(null);


    sendRequest.onreadystatechange = function () {
        if (sendRequest.readyState === 4 && sendRequest.status === 200) {
            let recData = sendRequest.responseText;
           // p.innerHTML = recData;
            if (recData == "CHARGING" && document.title == "Electric Dog")
                location.assign('./test.html');
            else if (recData == "NOT_CHARGING" && document.title == "I am EATING!")
                location.assign('./main.html');
        }
    };
}, 1000);